<?php
/**
 * Created by PhpStorm.
 * User: krasen
 * Date: 2015/9/5
 * Time: 17:00
 */
namespace AppBundle\Controller;

use AppBundle\Entity\User;
use AppBundle\Event\UserEvent;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use AppBundle\Form\RegisterForm;

class UserController extends Controller
{
    /**
     * @Route("/register", name="app_user_register")
     * @Template()
     * @param Request $request
     * @return array
     */
    public function registerAction(Request $request)
    {
        $formOptions = array(
            'action' => $this->generateUrl("app_user_register"),
            'method' => Request::METHOD_POST
        );
        $user = new User();
        $form = $this->createForm( new RegisterForm(), $user, $formOptions);
        if ($request->isMethod('POST')) {
            $em = $this->getDoctrine()->getManager();
            $form->submit($request);
            if ($form->isValid()) {
                $event = new UserEvent($user, $request);
                $dispatcher = $this->get('event_dispatcher');
                $dispatcher->dispatch(UserEvent::USER_EVENT_TEST, $event);
                $em->persist($user);
//                dump($user);exit;
                $em->flush();
            }
        }
        return ['form' => $form->createView()];
    }

    /**
     * @Route("/login")
     * @Template()
     * @return array
     */
    public function loginAction()
    {
        return [];
    }

    /**
     * @Route("/logout")
     * @Template()
     */
    public function logoutAction()
    {
        return [];
    }

    /**
     * @Route("/forgot-password")
     * @Template()
     */
    public function forgotPasswordAction()
    {
        return [];
    }

    /**
     * @Route("/active")
     * @Template()
     */
    public function activeAction()
    {
        return [];
    }
}
